// pages/login/login.js
import util from '../../../utils/util'
const app = getApp();
const http = app.http;
const userInfoManager = app.userInfoManager;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    color: '#000000',
    background: '#ffffff',
    title: "修改密码",
    changePwd: "",
    fromLogin: "",
    phone: "",
    captcha: "",
    newpwdValue: '',
    pwdValue: '',
    loginEnable: false,
    isShowPwd: '', //是否显示密码
    isShowNewPwd: ""
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.data.changePwd = options.changePwd;
    this.data.fromLogin = options.fromLogin;
    this.data.phone = options.phone;
    this.data.captcha = options.captcha;
    console.log(this.data.changePwd, this.data.fromLogin);
    if (this.data.changePwd === 'true' && this.data.fromLogin === 'true') {
      this.setData({
        title: "修改密码"
      });
    } else if (this.data.changePwd === 'false' && this.data.fromLogin === 'true') {
      this.setData({
        title: "设置新密码"
      });
    } else {
      this.setData({
        title: "修改密码"
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },
  /**
   * 是否显示密码
   * @param {*} e 
   */
  onClickEye(e) {
    this.setData({
      isShowPwd: !this.data.isShowPwd
    })
  },
  /**
   * 是否显示密码
   * @param {*} e 
   */
  onClickNewEye(e) {
    this.setData({
      isShowNewPwd: !this.data.isShowNewPwd
    })
  },
  /**
   * 确认修改
   */

  sure(e) {
    if (!this.data.loginEnable) return;
    if (this.data.fromLogin) { //第一次登录或者忘记密码
      http.findPassword(this.data.phone, this.data.captcha, this.data.pwdValue).then(res => {
        if (res.code == 0) {
          wx.showToast({
            duration: 1000,
            icon: 'none',
            title: '设置成功'
          });
          userInfoManager.clearAll();
          wx.reLaunch({
            url: '/pages/login-process/login/login?phone=' + this.data.phone,
            success: (result) => {

            },
            fail: () => {},
            complete: () => {}
          });
        } else {
          wx.showToast({
            duration: 1000,
            icon: 'none',
            title: res.msg
          })
        }
      })
    } else { //修改密码
      http.updatePassword(this.data.captcha, this.data.pwdValue).then(res => {
        if (res.code == 0) {
          wx.showToast({
            duration: 1000,
            icon: 'none',
            title: '修改成功'
          });
          userInfoManager.clearAll();
          wx.reLaunch({
            url: '/pages/login-process/login/login?phone=' + this.data.phone,
            success: (result) => {

            },
            fail: () => { },
            complete: () => { }
          });
        } else {
          wx.showToast({
            duration: 1000,
            icon: 'none',
            title: res.msg
          })
        }
      })
    }
  },
  onPwdInputListener(e) {
    let pwdValue = e.detail.value
    this.setData({
      pwdValue
    })
    this.canClickLogin();
  },
  onNewPwdInputListener(e) {
    let newpwdValue = e.detail.value
    this.setData({
      newpwdValue
    })
    this.canClickLogin();
  },
  /**
   * 按钮是否可以点击
   */
  canClickLogin() {
    console.log(this.data.pwdValue, this.data.newpwdValue);
    const b = this.data.pwdValue && this.data.pwdValue.length >= 6 && this.data.pwdValue === this.data.newpwdValue;
    this.setData({
      loginEnable: b
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },

})